package com.junmoo.www.auth.module.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junmoo.www.auth.module.system.mapper.SysMenuMapper;
import com.junmoo.www.entity.SysMenu;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@AllArgsConstructor
public class SysMenuService {

    private final SysMenuMapper sysMenuMapper;

    public List<SysMenu> findAll(){
        return sysMenuMapper.selectList(new QueryWrapper<SysMenu>());
    }

    public List<SysMenu> findByRoleId( Long roleId){
        return sysMenuMapper.findByRoleId(roleId);
    }

    public List<SysMenu> findByUserId( Long userId){
        return sysMenuMapper.findByUserId(userId);
    }

    private LambdaQueryWrapper<SysMenu> queryByAnd(SysMenu entity){
        return new LambdaQueryWrapper<SysMenu>()
                .eq(entity.getId()!=null,SysMenu::getId,entity.getId())
                .eq( StringUtils.hasText(entity.getMenuName()),SysMenu::getMenuName,entity.getMenuName())
                .eq( StringUtils.hasText(entity.getMenuCode()),SysMenu::getMenuCode,entity.getMenuCode())
                .eq( StringUtils.hasText(entity.getMenuPath()),SysMenu::getMenuPath,entity.getMenuPath());

    }

    private LambdaQueryWrapper<SysMenu> queryByOr(SysMenu entity){
        return new LambdaQueryWrapper<SysMenu>()
                .eq(entity.getId()!=null,SysMenu::getId,entity.getId())
                .or()
                .eq( StringUtils.hasText(entity.getMenuName()),SysMenu::getMenuName,entity.getMenuName())
                .or()
                .eq(StringUtils.hasText(entity.getMenuCode()), SysMenu::getMenuCode,entity.getMenuCode())
                .or()
                .eq(StringUtils.hasText(entity.getMenuPath()), SysMenu::getMenuPath,entity.getMenuPath());

    }

    public SysMenu findOneByAnd(SysMenu entity) {
        return sysMenuMapper.selectOne( queryByAnd(entity) );
    }
    public List<SysMenu> findByAnd(SysMenu entity) {
        return sysMenuMapper.selectList( queryByAnd(entity) );
    }

    public SysMenu findOneByOr(SysMenu entity) {
        return sysMenuMapper.selectOne( queryByOr(entity) );
    }
    public List<SysMenu> findByOr(SysMenu entity) {
        return sysMenuMapper.selectList( queryByOr(entity) );
    }

    public IPage<SysMenu> page(SysMenu search, int currentPage, int Pagesize,String sortField,String sortOrder) {
        Page<SysMenu> page = new Page<>(currentPage , Pagesize);
        OrderItem orderItem = "asc".equalsIgnoreCase(sortOrder)?OrderItem.asc(sortField):OrderItem.desc(sortField);
        List<OrderItem> orders = Stream.of( orderItem ).collect(Collectors.toList());
        page.setOrders(orders);
        return sysMenuMapper.selectPage( page, queryByOr(search) );
    }
//
//    @Transactional
//    public Result create(SysResource entity) {
//        SysResource repoEntity = sysResourceRepository.findByResourceCode(entity.getResourceCode());
//        if(repoEntity!=null) return Result.error("资源名已存在！！！");
//        // 排序字段设置
//        if(entity.getResourceSort() ==null || entity.getResourceSort() ==0){
//            String sql ="select max(resource_sort) from sys_resource where resource_type = 'menu'";
//            entity.setResourceType("menu");
//            if(entity.getParentId()!=0){
//                SysResource parent = sysResourceRepository.getOne(entity.getParentId());
//                if(parent.getResourceType().equalsIgnoreCase("menu")){
//                    sql ="select max(resource_sort) from sys_resource where resource_type = 'page'";
//                    entity.setResourceType("page");
//                }else {
//                    sql ="select max(resource_sort) from sys_resource where resource_type = 'button'";
//                    entity.setResourceType("button");
//                }
//            }
//            Query query = entityManager.createNativeQuery(sql);
//            int sort= Integer.valueOf( String.valueOf(query.getSingleResult()) ) +1;
//            entity.setResourceSort(sort);
//        }
//
//        SysResource returnEntity=sysResourceRepository.save(entity);
//        if(returnEntity==null) return Result.error("新建资源失败！！！");
//        return Result.success("success");
//    }
//    @Transactional
//    public Result edit(SysResource entity) {
//        SysResource repoEntity = sysResourceRepository.getOne(entity.getResourceId());
//        if(repoEntity==null) return Result.error("资源不存在！！！");
//        entity = BeanUtil.copyNullProperties(entity,repoEntity);
//        SysResource returnEntity=sysResourceRepository.save(entity);
//        if(returnEntity==null) return Result.error("资源不存在！！！");
//        return Result.success("success");
//    }
//    @Transactional
//    public Result deleteByIds(List<Number> ids) {
//        ids.removeAll(Collections.singleton(null));
//        if(CollectionUtils.isEmpty(ids)) return Result.error("error");
//        List<Long> list=ids.stream().map(id-> Long.valueOf(String.valueOf(id ))).collect(Collectors.toList());
//        sysResourceRepository.deleteAll( sysResourceRepository.findAllById(list) );
//        return Result.success("success");
//    }

}
